package com.loanly.controller;

import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat; 
import java.util.List;
import java.util.Map;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.classic.*;
//import org.hibernate.Session;
 
import com.loanly.util.HibernateUtil;
import com.opensymphony.xwork2.ActionContext;
 
public class SummaryManager extends HibernateUtil {
 
//总借出
    private Double sumAmount;
    private Double dueAmount;
    private Double assetAmount;
    private Double debitAmount;
    private Double curr_month_interest;
    private Double curr_month_award;
    private Double total_interest;
    private Double total_award;
	private String user_name;
	//private Session session = HibernateUtil.getSessionFactory().getCurrentSession();

	private String current_yearmonth;
	private String current_year;
	private String current_month;
	
	public SummaryManager(){
    	SimpleDateFormat df = new SimpleDateFormat("YYYYMM");
    	Date now = new Date();
    	
    	current_yearmonth = df.format(now);
    	current_year = current_yearmonth.substring(0, 4);
    	current_month = current_yearmonth.substring(4,6);
    	//user_name = getUser_name();
	}
	//借出总额
    public double getSumAmount() {
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    	//session = HibernateUtil.getSessionFactory().getCurrentSession();
        sumAmount = (double) 0;
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        
        System.out.println("user_name11 == " + user_name);
        String hql = "select sum(amount) from Loan where login_name = :login_name";
        session.getTransaction().begin();
        //session.beginTransaction();
        try {
        	Query query = session.createQuery(hql);
        	query.setParameter("login_name",user_name);
            List li = query.list();
            if (li == null || li.isEmpty()){
                session.getTransaction().commit();
            	return 0;
            }else{
            	if (li.get(0) == null){
                    session.getTransaction().commit();
            		return 0;
            	}
            }
            sumAmount  =(double) li.get(0);
        	
            session.getTransaction().commit();
        	System.out.println("sumAmount11 ==" + sumAmount);
             
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        }
        return sumAmount;
    }
//待收总额
    public double getDueAmount() {
        dueAmount = (double) 0;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        String hql = "select sum(amount) from Loan where status=0 and login_name = :login_name";
        //session.beginTransaction();
        session.getTransaction().begin();
        try {
        	Query query = session.createQuery(hql);
        	query.setParameter("login_name",user_name);
            List li = query.list();
            if (li == null || li.isEmpty()){
                session.getTransaction().commit();
            	return 0;
            }else{
            	if (li.get(0) == null){
                    session.getTransaction().commit();
            		return 0;
            	}
            }
            dueAmount  =(double) li.get(0);
        	
            session.getTransaction().commit();
        	System.out.println("dueAmount ==" + dueAmount);
             
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        }
        return dueAmount;
    }
    
    //assetamount = 资产总额=账户金额 + 借出待收金额
    public double getAssetAmount() {
        assetAmount = (double) 0;
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        double tempAmt;
        //tempAmt = getDueAmount();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        //session = HibernateUtil.getSessionFactory().getCurrentSession();
        String hql = "select sum(amount) from Assetaccount ";
        //where account_type in (0,2,3) and login_name = :login_name
        //session.beginTransaction();
        session.getTransaction().begin();
        try {
        	Query query = session.createQuery(hql);
        	//query.setParameter("login_name",user_name);
            List li = query.list();
            if (li == null || li.isEmpty()){
                session.getTransaction().commit();
            	return 0;
            }else{
            	if (li.get(0) == null){
                    session.getTransaction().commit();
            		return 0;
            	}
            }
            double tmpAmount  =(double) li.get(0);
        	
        	assetAmount = assetAmount + tmpAmount;
            session.getTransaction().commit();
        	System.out.println("assetAmount ==" + assetAmount);
             
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        }
        return assetAmount;
    }

  //debitamount = 总负债
    public double getDebitAmount() {
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        debitAmount = (double) 0;
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        String hql = "select sum(amount) from Assetaccount where account_type=1 and login_name = :login_name";
    	//session = HibernateUtil.getSessionFactory().getCurrentSession();
        //session.beginTransaction().begin();
        session.getTransaction().begin();
        try {
        	Query query = session.createQuery(hql);
        	query.setParameter("login_name",user_name);
            List li = query.list();
            if (li == null || li.isEmpty()){
                session.getTransaction().commit();
            	return 0;
            }else{
            	if (li.get(0) == null){
                    session.getTransaction().commit();
            		return 0;
            	}
            }
            debitAmount =(double) li.get(0);
        	
            session.getTransaction().commit();
             
        	System.out.println("debitAmount ==" + debitAmount);
             
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        }
        return debitAmount;
    }
    
  //净资产=总资产-负债 不需要从数据库计算
    
    //当月已赚利息
    public double getCurr_Month_Interest() {
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        curr_month_interest = (double) 0;
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        String hql = "select sum(interest_amt) from Loan where status=1 and year(return_date) = :current_year and month(return_date) = :current_month and login_name = :login_name";
        //session = HibernateUtil.getSessionFactory().getCurrentSession();
        //session.beginTransaction();
        session.getTransaction().begin();
        try {
        	Query query = session.createQuery(hql);
        	System.out.println("current_yearmonth ==" + current_yearmonth);
        	System.out.println("current_year ==" + current_year);
        	System.out.println("current_month ==" + current_month);
        	query.setParameter("login_name",user_name);
        	query.setParameter("current_year",Integer.parseInt(current_year));
        	query.setParameter("current_month",Integer.parseInt(current_month));
            List li = query.list();
            if (li == null || li.isEmpty()){
                session.getTransaction().commit();
            	return 0;
            }else{
            	if (li.get(0) == null){
                    session.getTransaction().commit();
            		return 0;
            	}
            }
            curr_month_interest =(double) li.get(0);
        	
            session.getTransaction().commit();
             
        	System.out.println("curr_month_interest ==" + curr_month_interest);
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        return curr_month_interest;
    }

    
  //当月奖励
    public double getCurr_Month_Award() {
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        curr_month_award = (double) 0;
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        String hql = "select sum(continue_amt)+sum(offline_amt) from Loan where status=1 and year(return_date) = :current_year and month(return_date) = :current_month and login_name = :login_name";
        //session = HibernateUtil.getSessionFactory().getCurrentSession();
        //session.beginTransaction();
        session.getTransaction().begin();
        try {
        	Query query = session.createQuery(hql);
        	query.setParameter("login_name",user_name);
        	query.setParameter("current_year",Integer.parseInt(current_year));
        	query.setParameter("current_month",Integer.parseInt(current_month));
            List li = query.list();
            if (li == null || li.isEmpty()){
                session.getTransaction().commit();
            	return 0;
            }else{
            	if (li.get(0) == null){
                    session.getTransaction().commit();
            		return 0;
            	}
            }
            curr_month_award =(double) li.get(0);
        	
            session.getTransaction().commit();
             
        	System.out.println("curr_month_award ==" + curr_month_award);
             
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        return curr_month_award;
    }    
    
    //当月收益 利息+奖励 不需要从数据库读取    

    //总收益 利息+奖励 不需要从数据库读取
    
  //总利息
    public double getTotal_Interest() {
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        total_interest = (double) 0;
        String hql = "select sum(interest_amt) from Loan where status=1 and login_name = :login_name";
        //session = HibernateUtil.getSessionFactory().getCurrentSession();
        //session.beginTransaction();
        session.getTransaction().begin();
        try {
        	Query query = session.createQuery(hql);
        	System.out.println("username xxx == "+ user_name);
        	query.setParameter("login_name",user_name);
            List li = query.list();
            if (li == null || li.isEmpty()){
                session.getTransaction().commit();
            	return 0;
            }else{
            	if (li.get(0) == null){
                    session.getTransaction().commit();
            		return 0;
            	}
            }
            total_interest =(double) li.get(0);
             
            session.getTransaction().commit();
        	System.out.println("total_interest ==" + total_interest);
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        return total_interest;
    }
    
  //总奖励
    public double getTotal_Award() {
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        total_award = (double) 0;
        String hql = "select sum(continue_amt)+sum(offline_amt) from Loan where status=1 and login_name = :login_name";
        //session = HibernateUtil.getSessionFactory().getCurrentSession();
        //session.beginTransaction();
        session.getTransaction().begin();
        try {
        	Query query = session.createQuery(hql);
        	query.setParameter("login_name",user_name);
            List li = query.list();
            if (li == null || li.isEmpty()){
            	return 0;
            }else{
            	if (li.get(0) == null){
            		return 0;
            	}
            }
        	total_award =(double) li.get(0);
            session.getTransaction().commit();
        	System.out.println("total_award ==" + total_award);
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        return total_award;
    }

    public double getMonth_amt(String year_month) {   //得到输入月份的待收金额
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        double tmp_amt = (double) 0;
        Integer pass_year = Integer.parseInt(year_month.substring(0,4));
        Integer pass_month = Integer.parseInt(year_month.substring(4,6));
        
        String hql = "select sum(amount) from Loan where status=0 and year(return_date) = :pass_year and month(return_date) = :pass_month and login_name = :login_name";
        //session = HibernateUtil.getSessionFactory().getCurrentSession();
        //session.beginTransaction();
        session.getTransaction().begin();
        try {
        	Query query = session.createQuery(hql);
        	query.setParameter("pass_year",pass_year);
        	query.setParameter("pass_month",pass_month);
        	query.setParameter("login_name",user_name);
            List li = query.list();
            if (li == null || li.isEmpty()){
                session.getTransaction().commit();
            	return 0;
            }else{
            	if (li.get(0) == null){
                    session.getTransaction().commit();
            		return 0;
            	}
            }
            tmp_amt =(double) li.get(0);
            session.getTransaction().commit();
        	System.out.println("getMonth_amt ==" + tmp_amt);
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        return tmp_amt;
	}
    
    public double getCredit_amt(String year_month) {   //得到输入月份的信用卡待还金额
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	user_name = attibutes.get("username").toString();
        double tmp_amt = (double) 0;
        Integer pass_year = Integer.parseInt(year_month.substring(0,4));
        Integer pass_month = Integer.parseInt(year_month.substring(4,6));
        
        String hql = "select sum(amount-paied_amt) from Credit_states where year(return_date) = :pass_year and month(return_date) = :pass_month and login_name = :login_name";
        //session = HibernateUtil.getSessionFactory().getCurrentSession();
        //session.beginTransaction();
        session.getTransaction().begin();
        try {
        	Query query = session.createQuery(hql);
        	query.setParameter("pass_year",pass_year);
        	query.setParameter("pass_month",pass_month);
        	query.setParameter("login_name",user_name);
            List li = query.list();
            if (li == null || li.isEmpty()){
                session.getTransaction().commit();
            	return 0;
            }else{
            	if (li.get(0) == null){
                    session.getTransaction().commit();
            		return 0;
            	}
            }
            tmp_amt =(double) li.get(0);
            session.getTransaction().commit();
        	System.out.println("getCredit_amt ==" + tmp_amt);
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        return tmp_amt;
	}

    public String getUser_name() {
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
    	this.user_name = attibutes.get("username").toString();
    	System.out.println("user_name == " + user_name);
		return user_name;
	}
	public void setUser_name(String user_name) {
    	Map attibutes = (Map) ActionContext.getContext().getSession(); 
		this.user_name = attibutes.get("username").toString();
	}
    
}